package com.fenger.bus.controller;


import com.fenger.bus.service.GoodsService;
import com.fenger.bus.vo.GoodsVo;
import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.utils.DataGirdView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GoodsController {
    private final static String CUSTOMER_PREFIX = "goods/";

    @Autowired
    GoodsService goodsService;

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "loadAllGoods")
    private DataGirdView loadAllGoods(GoodsVo goodsVo) {
        return this.goodsService.queryAllGoods(goodsVo);
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "loadSelectedProviderList")
    private DataGirdView loadSelectedProviderList() {
        return this.goodsService.querySelectedProviderList();
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "addGoods")
    private DataGirdView addGoods(GoodsVo goodsVo) {
        try {
            this.goodsService.addGoods(goodsVo);
            return new DataGirdView(SysConstast.ADD_SUCCESS_CODE, SysConstast.ADD_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.ADD_ERROR_CODE, SysConstast.ADD_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "updateGoods")
    private DataGirdView updateGoods(GoodsVo goodsVo) {

        try {
            this.goodsService.updateGoods(goodsVo);
            return new DataGirdView(SysConstast.UPDATE_SUCCESS_CODE, SysConstast.UPDATE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.UPDATE_ERROR_CODE, SysConstast.UPDATE_ERROR_MSG, 0L, null);
        }
    }

    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteGoods")
    private DataGirdView deleteGoods(GoodsVo goodsVo) {

        try {
            this.goodsService.deleteGoods(goodsVo.getId());
            return new DataGirdView(SysConstast.DELETE_SUCCESS_CODE, SysConstast.DELETE_SUCCESS_MSG, 1L, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DELETE_ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
        }
    }


    @ResponseBody
    @RequestMapping(CUSTOMER_PREFIX + "deleteListGoods")
    private DataGirdView deleteListGoods(@RequestParam(value="id[]", required = false) String[] id) {
        System.out.println("--------------------");
        System.out.println(id);
        for (int i = 0; i < id.length; i++) {
            String s = id[i];
            System.out.println(s);
        }
        return new DataGirdView(SysConstast.ERROR_CODE, SysConstast.DELETE_ERROR_MSG, 0L, null);
    }

}
